<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
	//首页
	public function getLayout()
	{
		//解析模板
		return view('admin.layout.layout');
	}

	public function getList(Request $request)
	{
		//查询所有用户数据
		// $users=DB::table('user')->get();
		// dd($users);
		//获取每页显示几条
		$num=$request->input('num',10);
		//判断是否有查询条件
		if($request->input('keywords')){
			$users=DB::table('users') -> where('username','like','%'.$request->input('keywords').'%')->paginate($num);	
		}else{
			$users=DB::table('users')->paginate($num);
		}
		//获取参数列表
		$list=$request->all();
		//解析模板
		return view('Admin.user.list',['users'=>$users,'list'=>$list]);
	}

	public function getAdd()
	{
		//解析模板
		return view('admin.user.add');
	}

	//进行数据库添加操作
	public function postInsert(UserInsertRequest $request)
	{
		//提取数据
		$data=$request->only(['username','password','email','phone']);
		//处理token
		$data['token']=str_random(50);
		//注册时间
		$data['regtime']=time();
		//密码处理 加密
		$data['password']=Hash::make($data['password']);
		$data['userauth']=3;
		//调用函数处理头像上传
		$data['pic']=$this->upload($request,'pic');
		if(empty($data['pic'])){
			$data['pic']='/uploads/35621203e1cc3bbc8b78c22668d8f40b.jpg';
		}
		//执行数据插入操作
		$res=DB::table('users')->insert($data);
		if($res){
			//成功 到列表页
			return redirect('/admin/user/list')->with('success',"用户添加成功");		
		}else{
			return back()->with('error','用户添加失败');
		}

	}

	//封装方法 进行头像上传处理
	private function upload($request,$filename)
	{
		if($request->hasFile($filename)){
			//获取文件后缀名
			$suffix=$request->file($filename)->getClientOriginalExtension();
			//随机文件名
			$name=md5(time().rand(1,9999));
			$request->file($filename)->move('./uploads/',$name.'.'.$suffix);
			return '/uploads/'.$name.'.'.$suffix;
		}
	}

	//封装方法 进行ajax删除操作
	public function getDelete(Request $request)
	{
		$uauth=$this->User();

		$uid=$request->input('uid');
		if($uauth==4){
			//删除用户
			$res=DB::table('users')->where('uid',$uid)->delete();
			echo $res;
		}else{
			echo 5;
		}
	}

	//封装方法进行ajax提权操作
	public function getUp(Request $request)
	{
		$uauth=$this->User();
		$uid=$request->input('uid');
		$user=DB::table('users')->where('uid',$uid)->first();
		if($uauth ==4){
			if($user->userauth < 4 ){
				$userauth=$user->userauth+1;
				$res=DB::table('users')->where('uid',$uid)->update(['userauth' => $userauth]);
				// $arr=['userauth'=>$userauth,'res'=>$res];
				// echo json_encode($arr);
				echo $res;
			}else{
				echo 0;
			}	
		}else{
			echo 5;
		}
	}

	//封装方法进行ajax降权操作
	public function getDown(Request $request)
	{
		$uauth=$this->User();
		$uid=$request->input('uid');
		$user=DB::table('users')->where('uid',$uid)->first();
		if($uauth ==4){
			if($user->userauth > 0 && $uauth ==4){
				$userauth=$user->userauth-1;
				$res=DB::table('users')->where('uid',$uid)->update(['userauth' => $userauth]);
				echo $res;
			}else{
				echo 0;
			}	
		}else{
			echo 5;
		}
	}


	//封装方法进行修改显示
	public function getEdit(Request $request)
	{
		
		$id=$request->input('uid');
		//查询这个用户
		$users=DB::table('users')->where('uid',$id)->first();
		//解析模板
		return view('admin/user/edit',['users'=>$users]);

	}
	//处理ajax发送的用户名
	public function getCheck(Request $request)
	{
		
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('username',$user['username'])->first();

		// dd($res);
		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
		
	}

	//处理ajax发送的手机号
	public function getPhone(Request $request)
	{
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('phone',$user['phone'])->first();

		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
	}

	//处理ajax发送的Email
	public function getEmail(Request $request)
	{
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('email',$user['email'])->first();

		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
	}

	//进行数据修改
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		if(empty($request->input('password'))){
			$data=$request->only(['username','email','phone']);	
		}else{
			$data=$request->only(['username','password','email','phone']);
			//密码处理 加密
			$data['password']=Hash::make($data['password']);
		}
				
		//判断是否有头像上传
		if($request->hasFile('pic')){
			$data['pic']=$this->upload($request,'pic');

		}

		//获取id
		$id=$request->input('uid');
		//进行数据修改操作
		$res=DB::table('users')->where('uid',$id)->update($data);
		if($res){
			//成功 到列表页
			return redirect('/admin/user/list')->with('success','用户修改成功');
		}else{
			return back()->with('error','用户修改失败');
		}

	}

	//登录用户的信息
	private function User()
	{
		$res=DB::table('users')->where('uid',session('uid'))->first();
		return $res->userauth;
	}



}
